<?php
/**
 * 
 * @author Rajat
 * Class that generates the thumbnails
 */
class thumbnail{

  const IMAGE_URI = 'http://webthumb.bluga.net/easythumb.php?user=';
  const FAVICON = '<img src="http://www.google.com/s2/favicons?domain=';
  
  function __construct($title=null,$alttext=null) {
    $this->title = $title;
    $this->alttext = $alttext;
    $this->requestObject = new CurlRequest();
  }

  public function getthumbblock(){
    $favicon = $this->getFavicon();
    $imgTitle = $this->getImageTitle();
    $imgThumb = $this->getImageThumbnail();
    $thumbblock = '<div class="thumbblock">' .$favicon.$imgTitle.$imgThumb .'</div>';
    return $thumbblock;
  }

  private function getFavicon(){
    $temp = str_replace('http://','',$this->title);
    $temp = substr($temp,0,strpos($temp,'/'));
    $favicon =  thumbnail::FAVICON . $temp . '" align="left" vspace="1" class="favicon"/>';
    return $favicon;
  }

  private function getImageTitle(){
    return '<span class="thumbTitle"><a href="'. $this->title .'">' . $this->title . '</a></span>';
  }

  private function getImageThumbnail(){
    //check if image exists on disk else save image to disk
    global $width,$height;
    $src = $this->searchImage();
    $imgHtml  = '<img class="thumbnail" src="images/' . $src . '.jpg" alt="'. $this->alttext .'" width="'.$width.'" height="'.$height.'"/>';
    return $imgHtml;
  }

  private function searchImage(){
    global $xml;
    $xpath = '/images/image[url="'.$this->title .'"]/uid';
    $result = $xml->xpath($xpath);
    if( $result === false ){    //no image in the xml indicates no image thumnail on disk
      $uid  = $this->generateUid($this->title);
      if( $this->fetchImagefromWebthumbandSave($uid) === false){
        die('error in webthumb');
      }
      else{
        $image = $xml->addChild('image');
        $image->addChild('url',$this->title);
        $image->addChild('uid',$uid);
        $xml->asXML("index.xml");
      }
    }
    else{
      $uid = (string) $result[0];
    }
    return $uid;
  }

  private function fetchImagefromWebthumbandSave($uid){
    global $api_key,$size,$user_id,$cache;
    $urlencode = urlEncode($this->title);
    $hash = md5(gmdate('Ymd').$this->title.$api_key);
    $imageSrc = thumbnail::IMAGE_URI .$user_id.'&url='.$urlencode.'&size='.$size.'&cache='.$cache.'&hash='.$hash;
    $res = $this->requestObject->fetchRequest($imageSrc);
    if(strcmp($res,'Bad Hash') === 0){
      //die('error in webthumb' . $res );
      return false;
    }
    else{
      $loc = 'images/'.$uid .'.jpg';
      $fp = fopen($loc,'w');
      fwrite($fp, $res);
      fclose($fp);
      return true;
    }
  }

  private function generateUid(){
    $urlhash = '' . md5($this->title);
    $urlhash = str_shuffle($urlhash);
    $start = rand(0,strlen($urlhash)-8);
    $uid = substr($urlhash,$start,$start + 8);
    return $uid;
  }

  private $title = null;
  private $alttext = null;
}
?>